<?php

namespace App\Handler;

use App\Facades\JQEncrypt;
use App\Models\AdminRole;
use App\Models\Dic;
use App\Models\GlobalEventNotice;
use App\Models\RoleMenu;
use App\Models\UserNoticeHistory;
use App\Utils\CollectionUtils;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/12
 * Time: 9:36
 */
class Dics
{

    const CACHE_KEY = "ggw.all.dics";

    static public function all()
    {
        if (Cache::has(Dics::CACHE_KEY)) {
            return Cache::get(Dics::CACHE_KEY);
        }
        $sql = "select * from dic";
        $temp = DB::select($sql);
        $list = json_decode(json_encode($temp), true);
        Cache::forever(Dics::CACHE_KEY, $list);
        return $list;
    }

    static public function lookup($dic_name, $dic_key)
    {
        $map = self::lookIntoMap($dic_name);
        if (array_key_exists($dic_key, $map)) {
            return $map[$dic_key];
        }
        return $dic_key;
    }

    static public function findLine($dic_name, $dic_key)
    {
        $list = self::all();
        foreach ($list as $line) {
            if ($line["dic_name"] == $dic_name) {
                if ($line["dic_key"] == $dic_key) {
                    return $line;
                }
            }
        }
        return [];
    }

    static public function lookIntoMap($dic_name)
    {
        $list = self::all();
        $result = [];
        foreach ($list as $line) {
            if ($line["dic_name"] == $dic_name) {
                $result[$line["dic_key"]] = $line["dic_value"];
                // array_push($result, $line);
            }
        }
        return $result;
    }

    static public function lookIntoList($dic_name)
    {
        $list = self::all();
        $result = [];
        foreach ($list as $line) {
            if ($line["dic_name"] == $dic_name) {
                array_push($result, $line);
            }
        }
        return $result;
    }

    static public function clear()
    {
        Cache::forget(Dics::CACHE_KEY);
    }

}